home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
14642
/
14642.xpi
/
chrome
/
content
/
historyPiechart.js
< prev
next >
Wrap
Text File
|
2009-09-22
|
9KB
|
224 lines
/* Copyright 2009, Boomtango.com. All Rights Reserved. */
/* historyPiechart.js
* Responsible for piechart view
*/
bthistory.controllers["piechart"] = {
handleUpArrow: function(){
},
handleDownArrow: function(){
},
onHistoryAdd: function(data) {
},
onHistoryChange: function(data) {
},
handleResize: function(){
},
queryTracker: function(){
return bthistory.storage.querySummary(
bthistory._range.start,
bthistory._range.end
);
},
loadView: function(){
document.loadOverlay("chrome://boomtango/content/historyPiechart.xul", this);
},
observe: function(subject, topic, data) {
if(topic == "xul-overlay-merged"){
bthistory.app.log("historyPiechart::overlayLoaded");
this.loadMergedView();
}
},
/*
loadMergedView is handled after view has been merged.
*/
loadMergedView: function() {
bthistory.app.log("historyPiechart::loadMergedView");
var data = bthistory._data;
var body = document.getElementById("body");
if(data.types.length == 0){
var label= document.createElement("label");
label.setAttribute("value", bthistory.app.getString("history.nodatafound"));
label.className = "nodatafound";
body.appendChild(label);
document.getElementById("piebox").setAttribute("hidden", "true");
} else {
this.loadPiechart(data.types);
var types = bthistory.tracker.types;
for(var x in types){
if(data.urls.hasOwnProperty(x) &&
bthistory.app.getTrackerEnabled(x)){
this.loadTopSites(x, data.urls[x]);
}
}
}
document.getElementById("btfilter_deck").setAttribute("hidden", "true");
document.getElementById("bubble_back").setAttribute("hidden", "true");
},
loadTopSites: function(type, data){
if(data && data.length){
var name = bthistory.app.tracker.types[type].name_plural;
var len = data.length;
var container = document.getElementById("urlsummary");
var box = document.createElement("vbox");
box.className = "topurlbox";
box.setAttribute("style", "border-color: " +
bthistory.app.getTrackerColor(type) + ";");
var title = bthistory.app.getString("pie.type.topurls", name);
var label = document.createElement("label");
label.className = "topurl_title";
label.setAttribute("value", title);
box.appendChild(label);
//box.setAttribute("flex", "1");
var fi = Components.classes["@mozilla.org/browser/favicon-service;1"].getService(Components.interfaces.nsIFaviconService);
var io = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService2);
for(var x = 0; x < data.length; x++){
var item = data[x];
var hbox = document.createElement('hbox');
hbox.setAttribute("flex", "1");
hbox.setAttribute("style", "background-color: transparent;");
hbox.setAttribute("contentID", item.ftsrowid);
var uri = io.newURI(item.url, null, null);
var iconURI = fi.getFaviconImageForPage(uri);
var img = document.createElement("image");
img.setAttribute("src", iconURI.spec);
img.className = "calendaricon";
img.setAttribute("style", "background-color: transparent;");
var title = item.title || item.url;
var vbox = document.createElement('vbox');
vbox.setAttribute("style", "background-color: transparent;");
var linkNode = document.createElement("label");
linkNode.className = "historyItem";
linkNode.setAttribute("value", title);
linkNode.setAttribute("crop", "end");
linkNode.setAttribute("flex", "1");
vbox.appendChild(img);
hbox.appendChild(vbox);
hbox.appendChild(linkNode);
label = document.createElement("label");
label.className = "topurl";
if(item.timespent){
label.setAttribute("value", bthistory.duration(item.timespent));
}
hbox.appendChild(label);
box.appendChild(hbox);
}
container.appendChild(box);
}
},
loadPiechart: function(data){
bthistory.app.log("piechart::loadPiechart");
const width = 200;
const height = 200;
const center = width / 2;
const radius = center;
var canvas = document.getElementById('pie');
canvas.style.width = width + "px";
canvas.style.height = height + "px";
canvas.width = width;
canvas.height = height;
var len = data.length;
var total = 0;
var other = 0;
var webindex = -1;
for(var x = 0; x < len; x++){
if(data[x].type == 'web'){
webindex = x;
total = data[x].timespent;
} else if(bthistory.app.getTrackerEnabled(data[x].type)){
other += data[x].timespent;
}
}
data[webindex].timespent = total - other;
var curr = 0;
var ctx = canvas.getContext('2d');
for(var x = 0; x < len; x++){
var val = data[x].timespent / total;
ctx.beginPath();
ctx.moveTo(center, center);
ctx.arc(
center,
center,
radius,
Math.PI * (-0.5 + 2 * curr),
Math.PI * (-0.5 + 2 * (curr + val)),
false
);
ctx.lineTo(center, center);
ctx.closePath();
ctx.fillStyle = bthistory.app.getTrackerColor(data[x].type);
ctx.fill();
ctx.restore();
curr += val;
}
// load text
var label = document.createElement('label');
label.setAttribute('value', bthistory.duration(total) + " (100%)");
label.className = "typeSummaryTop";
var titlebox = document.getElementById("typesummary_title");
var databox = document.getElementById("typesummary_data");
databox.appendChild(label);
if(len > 1){
data.sort(function(a,b) { return b.timespent - a.timespent;});
var func = function(type, timespent){
var hbox = document.createElement("hbox");
var label = document.createElement('label');
var name;
if(type == 'web'){
name = bthistory.app.getString("pie.type.other");
} else if (bthistory.app.tracker.types[type]){
name = bthistory.app.tracker.types[type].name_plural;
} else {
name = bthistory.app.getString("tracker.unknown");
}
label.setAttribute('value', bthistory.app.getString("pie.type.timespent", name));
label.className = "typeSummary";
label.setAttribute("style", "color: " + bthistory.app.getTrackerColor(type) + ";");
hbox.appendChild(label);
titlebox.appendChild(label);
label = document.createElement('label');
label.className = "typeSummary";
label.setAttribute("value",
bthistory.duration(timespent) + " (" + Math.floor(timespent * 100 / total) + "%)");
databox.appendChild(label);
};
var otherspent = 0;
for(var x = 0; x < len; x++){
var type = data[x].type;
var timespent = data[x].timespent;
if(type == "web"){
otherspent = timespent;
} else if(bthistory.app.getTrackerEnabled(type)){
func(type, timespent);
}
}
func('web', otherspent);
}
},
QueryInterface: function(iid) {
if (iid.equals(this.ci.nsIObserver) ||
iid.equals(this.ci.nsISupports)) {
return this;
}
throw Components.result.NS_ERROR_NO_INTERFACE;
}
};